package team.knowers.qzhserver.course.datum.file.service.Impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import team.knowers.qzhserver.common.service.RedisService;
import team.knowers.qzhserver.course.datum.file.entity.dao.FileDO;
import team.knowers.qzhserver.course.datum.file.service.FileCacheService;

import java.util.HashSet;
import java.util.Set;

/**
 * @author zcb
 * @date 2024/3/9 23:30
 */
@Service
public class FileCacheServiceImpl implements FileCacheService {
    @Autowired
    private RedisService redisService;
    @Value("${redis.expire.common}")
    private Long REDIS_EXPIRE;
    @Value("${redis.key.file}")
    private String REDIS_KEY_FILE;
    @Value("${redis.key.datumFileSet}")
    private String REDIS_KEY_DATUM_FILE_SET;

    @Override
    public void setFile(FileDO fileDO) {
        String key = REDIS_KEY_FILE + ":"+fileDO.getId();
        redisService.set(key,fileDO,REDIS_EXPIRE);
    }

    @Override
    public FileDO getFile(String id) {
        String key = REDIS_KEY_FILE + ":" + id;
        return (FileDO) redisService.get(key);
    }

    @Override
    public void delFile(String id) {
        String key = REDIS_KEY_FILE + ":" + id;
        redisService.del(key);
    }

    @Override
    public void setDatumFileSetAll(String cdid, Set<String> data) {
        String key = REDIS_KEY_DATUM_FILE_SET+":"+cdid;
        redisService.sAdd(key,REDIS_EXPIRE,data.toArray());
    }

    @Override
    public void setDatumFileSet(String cdid, String id) {
        String key = REDIS_KEY_DATUM_FILE_SET+":"+cdid;
        redisService.sAdd(key,REDIS_EXPIRE,id);
    }

    @Override
    public void delDatumFileSetAll(String cdid) {
        String key = REDIS_KEY_DATUM_FILE_SET+":"+cdid;
        redisService.del(key);
    }

    @Override
    public void delDatumFileSet(String cdid, String id) {
        String key = REDIS_KEY_DATUM_FILE_SET+":"+cdid;
        redisService.sRemove(key,id);
    }

    @Override
    public Set<String> getDatumFileSet(String cdid) {
        String key = REDIS_KEY_DATUM_FILE_SET+":"+cdid;
        Set<String> output = new HashSet<>();
        for(Object obj:redisService.sMembers(key)){
            output.add(obj.toString());
        }
        return output;
    }
}
